package com.mrd.evaluate.service;

import com.mrd.base.BaseBiz;
import com.mrd.evaluate.entity.EvaluationRecord;
import com.mrd.spring.repository.BaseRepository;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2016/8/30.
 */
@Service
public class EvaluationRecordBiz extends BaseBiz<EvaluationRecord> {

    public void saveOrupdate(String relationId, String pId, String evalId, String evalContent, int evalScore, String evalPerson, String evalPersonId) throws Exception {
        if (!StringUtils.isEmpty(relationId) && !StringUtils.isEmpty(evalContent) && !StringUtils.isEmpty(evalPerson) && !StringUtils.isEmpty(evalPersonId)) {
            if (StringUtils.isEmpty(evalId)) {
                evalId = UUID.randomUUID().toString();
                String sql = "INSERT INTO p_evaluation t VALUES (:EVAL_ID_,:RALATION_ID_,:EVAL_SCORE_,:STATE_)";
                Map<String, Object> parameters = new HashMap<>();
                parameters.put("EVAL_ID_", evalId);
                parameters.put("RALATION_ID_", relationId);
                parameters.put("EVAL_SCORE_", evalScore);
                parameters.put("STATE_", "0");
                super.excuteSql(sql, parameters);
            }

            if (StringUtils.isEmpty(pId)) {
                pId = "";
            }
            String sql = "INSERT INTO p_evaluation_record T VALUES(:RECORD_ID_,:P_ID_,:EVAL_ID_,:EVAL_CONTENT_,SYSDATE,:EVAL_PERSON_,:EVAL_PERSON_ID_)";
            Map<String, Object> parameters = new HashMap<>();
            String recordId = UUID.randomUUID().toString();
            parameters.put("RECORD_ID_", recordId);
            parameters.put("P_ID_", pId);
            parameters.put("EVAL_ID_", evalId);
            parameters.put("EVAL_CONTENT_", evalContent);
            parameters.put("EVAL_PERSON_", evalPerson);
            parameters.put("EVAL_PERSON_ID_", evalPersonId);
            super.excuteSql(sql, parameters);
        }
    }

    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("evaluationRecordDao")
    public void setBaseRepository(BaseRepository<EvaluationRecord, String> baseRepository) {
        super.baseRepository = baseRepository;
    }
}
